<template>
  <div class="Terminal_box" ref="boxTheSize">
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-light">
          <div class="tltle_box">
            <el-row :gutter="24" style="width: 100%">
              <el-col :sm="24" :md="12" :lg="14">
                <div class="tltle_box_l">
                  <div class="Material_r">
                    <el-row :gutter="24" style="width: 100%; margin: 0">
                      <el-col
                        :sm="24"
                        :md="16"
                        :lg="16"
                        style="display: flex; padding: 5px!important;"
                      >
                        <el-input
                          placeholder="请输入关键字"
                          v-model="inputText"
                          @clear="seektableList"
                          clearable
                        ></el-input>
                        <el-button
                          type="primary"
                          icon="el-icon-search"
                          @click="seektableList"
                          >搜索</el-button
                        >
                      </el-col>
                      <el-col
                        :sm="24"
                        :md="24"
                        :lg="24"
                        style="padding: 5px; display: flex"
                      >
                        <el-button class="refresh" style="margin: 0" @click="openAdvancedSearch"
                          >高级搜索</el-button
                        >
                        <div class="refreshFor">
                          <el-tooltip
                            v-for="(item, key, index) in advancedSearchList"
                            placement="top"
                            :key="index"
                            v-if="item"
                          >
                            <div slot="content">{{ item }}</div>
                            <el-tag closable @close="closableTag(key)">
                              <span class="refreshFor_li_text">{{ item }}</span>
                            </el-tag>
                          </el-tooltip>
                        </div>
                      </el-col>
                    </el-row>
                  </div>
                </div>
              </el-col>
              <el-col :sm="24" :md="12" :lg="10">
                <div class="tltle_box_r">
                  <el-date-picker
                    v-model="tiemValue"
                    type="datetimerange"
                    @change="seektableList"
                    range-separator="至"
                    start-placeholder="开始日期"
                    :clearable="false"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :default-time="['00:00:00', '23:59:59']"
                  ></el-date-picker>
                </div>
              </el-col>
            </el-row>
          </div>
          <div class="Terminal_table">
            <zycm-table
              @zyLoadData="loadData"
              zy-table-ref="media_group_runinglog_table"
              :zy-table-data="tabledataList"
              zy-table-height="60vh"
            >
              <el-table-column
                label="终端名称"
                prop="clientName"
                min-width="100"
                show-overflow-tooltip
              ></el-table-column>
              <el-table-column
                label="终端组名称"
                prop="mediaGroupName"
                 min-width="100"
                show-overflow-tooltip
              ></el-table-column>
              <el-table-column label="日志数" prop="logNum" width="80" show-overflow-tooltip></el-table-column>
              <el-table-column label="操作" width="100">
                <template slot-scope="scope">
                  <el-button
                    @click="
                      LogDetailsShow(scope.row);
                      LogDetails = true;
                    "
                    type="primary"
                    :size="GlobalVar.sizeSet.btn"
                    >查看</el-button
                  >
                </template>
              </el-table-column>
            </zycm-table>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 高级搜索 -->
    <el-dialog
      title="高级搜索"
      :visible.sync="AdvancedSearch"
      :width="this.$store.getters.offsetWidth"
      v-dialogDrag
      ref="dialog__wrapper"
      :close-on-click-modal="false"
      
    >
    <!-- @close="empty(1)" -->
      <div class="dialog-body">
        <div class="line">
          <div class="Advanced_head">
            <div>
              <el-select
                placeholder="请选择日志类型"
                v-model="advancedSearch.SearchType"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
            <div>
              <el-input
                placeholder="大于日志数"
                v-model="advancedSearch.greaterThan"
              ></el-input>
            </div>
          </div>
          <div></div>
          <span
            style="
              width: 100%;
              text-align: center;
              margin: 20px auto 0;
              height: 50px;
              display: block;
            "
            class="button_dialog"
          >
            <el-button
              style="background-color: #4d9ce0; color: #fff"
              @click="advancedTableList"
              >查 询</el-button
            >
            <el-button @click="empty">清 空</el-button>
          </span>
        </div>
      </div>
    </el-dialog>
    <!-- 日志详情 -->
    <el-dialog
      :title="logData.clientName ? logData.clientName + '--日志详情' : ''"
      :visible.sync="LogDetails"
      :width="this.$store.getters.offsetWidth"
      class="LogDetails"
      v-dialogDrag
      ref="dialog__wrapper"
      :close-on-click-modal="false"
      @close="closeLogDetails"
    >
      <div class="dialog-body">
        <div class="line">
          <zycm-table
            @zyLoadData="loadData"
            zy-table-ref="media_group_runinglog_LogDetails"
            :zy-table-data="tabledataList"
            zy-table-height="400px"
            zy-border
            zy-layout="prev,pager, next,total,sizes"
          >
            <el-table-column label="日志时间" prop="logTime" width="140" show-overflow-tooltip>
              <template slot-scope="scope">
                <div>{{ scope.row.logTime | dateFormat }}</div>
              </template>
            </el-table-column>
            <el-table-column label="级别" prop="logLevel" width="120" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-tag type="danger" v-show="scope.row.logLevel == 2" :size="GlobalVar.sizeSet.tag"
                  >错误异常</el-tag
                >
                <el-tag type="success" v-show="scope.row.logLevel == 1" :size="GlobalVar.sizeSet.tag"
                  >普通</el-tag
                >
                <el-tag type="warning" v-show="scope.row.logLevel == 3" :size="GlobalVar.sizeSet.tag"
                  >警告</el-tag
                >
                <el-tag v-show="scope.row.logLevel == 20" :size="GlobalVar.sizeSet.tag">LED普通</el-tag>
                <el-tag type="danger" v-show="scope.row.logLevel == 21" :size="GlobalVar.sizeSet.tag"
                  >LED异常</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column label="日志信息" prop="logInfo" show-overflow-tooltip min-width="100"></el-table-column>
          </zycm-table>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      query: {
        page: 1,
        pageSize: this.GlobalVar.query.pageSize,
        total: 0,
      },
      query1: {
        page: 1,
        pageSize: this.GlobalVar.query.pageSize,
        total: 0,
      },
      AdvancedSearch: false,
      LogDetails: false,
      // 搜索字段
      inputText: "",
      TerminalHeight: "",
      tableData: [],
      options: [
        {
          label: "离线日志",
          value: 1,
        },
        {
          label: "LED更新失败",
          value: 2,
        },
        {
          label: "下载日志",
          value: 3,
        },
        {
          label: "异常日志",
          value: 4,
        },
      ],
      tiemValue: [],
      advancedSearch: {
        SearchType: "",
        greaterThan: "",
      },
      advancedSearchList: {},
      particularsTable: [],
      logData: "",
      thisFormData: new FormData(),
      tabledataList: [], //保存当页的所有table数据
      tablePageList: [], //保存对应table的page数据
    };
  },

  created() {
    this.clientHeight();
    this.tiemValue.push(
      this.dateFormat(new Date()).substring(0, 10) + " 00:00:00"
    );
    this.tiemValue.push(
      this.dateFormat(new Date()).substring(0, 10) + " 23:59:59"
    );
    let fd = new FormData();
    fd.set("startTime", this.tiemValue[0]);
    fd.set("endTime", this.tiemValue[1]);
    this.currentTableLoad("media_group_runinglog_table", true, fd);
    // this.tableList();
  },
  watch: {
    tiemValue(value) {
      if (!value) {
        this.tiemValue = [];
        this.tiemValue.push(
          this.dateFormat(new Date()).substring(0, 10) + " 00:00:00"
        );
        this.tiemValue.push(
          this.dateFormat(new Date()).substring(0, 10) + " 23:59:59"
        );
        // this.query.page = 1;
        // this.query.pageSize = 10;
      }
    },
  },
  methods: {
    openAdvancedSearch() {
      let SearchType = "";
      /* this.options.forEach((element) => {
        if (element.label == this.advancedSearchList.SearchType) {
          SearchType = element.value;
        }
      }); */
      if (this.advancedSearchList) {
        this.advancedSearch = {
          greaterThan: this.advancedSearchList.greaterThan || "",
          SearchType: this.advancedSearchList.SearchType || "",
        };
      }
      this.AdvancedSearch = true;
    },
    //给子组件刷新用
    async loadData(formData, tableRef) {
      //提前修正isReqData状态 后续赋值随时会触发table组件的computed  后面还要进行一次修正
      for (let key in this.tabledataList) {
        if (this.tabledataList[key].ref === tableRef) {
          this.tabledataList[key].isReqData = true; //标记更新数据的状态 组件中会根据此判断是否重新更新数据
        } else {
          this.tabledataList[key].isReqData = false;
        }
      }

      //子组件触发查询时  处理查询的参数
      if (this.tablePageList[tableRef]) {
        this.tablePageList[tableRef].set("page", formData.get("page"));
        this.tablePageList[tableRef].set("pageSize", formData.get("pageSize"));
      }

      let res;
      if (tableRef == "media_group_runinglog_table") {
        //发布-选择节目列表
        res = await this.$http.post(
          "runLog/pageStatisticsRunLog",
          this.tablePageList[tableRef]
        );
        // this.ProgramSelectList = [];
      } else if (tableRef == "media_group_runinglog_LogDetails") {
        res = await this.$http.post(
          "runLog/selectPage",
          this.tablePageList[tableRef]
        );
      }

      if (res && res.data.result === "success") {
        //记录更新分页数据
        res.data.ref = tableRef;
        //更新记录
        let sign = 0;
        for (let key in this.tabledataList) {
          if (this.tabledataList[key].ref === tableRef) {
            if (sign > 1) {
              //去掉重复数据
              this.tabledataList.splice(key, 1);
            } else {
              this.tabledataList.splice(key, 1, res.data);
              this.tabledataList[key].isReqData = true; //标记更新数据的状态 组件中会根据此判断是否重新更新数据
            }
            sign++;
          } else {
            this.tabledataList[key].isReqData = false;
          }
        }
        if (sign == 0) {
          //没有初始化时
          res.data.isReqData = true;
          this.tabledataList.push(res.data);
        }
      } else {
        if (res && res.data.result === "error") {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        } else {
          this.$message({
            type: "error",
            message: "请求数据出现异常",
          });
        }
      }
    },
    //当页刷新数据
    currentTableLoad(tableRef, firstPage, fd) {
      if (firstPage) {
        //加载第一页数据
        if (!this.tablePageList[tableRef]) {
          this.tablePageList[tableRef] = new FormData();
          this.tablePageList[tableRef].set("pageSize", this.GlobalVar.query.pageSize);
        }
        this.tablePageList[tableRef].set("page", 1);
      }
      //如果有其他查询参数 合并处理
      if (fd) {
        for (let item of fd.entries()) {
          this.tablePageList[tableRef].set(item[0], item[1]);
        }
      }
      this.loadData(this.tablePageList[tableRef], tableRef);
    },

    seektableList() {
      this.tableList();
    },
    closeLogDetails() {
      this.particularsTable = [];
      this.query1.page = 1;
      this.query1.pageSize = this.GlobalVar.query.pageSize;
    },
    advancedTableList() {
      this.query.page = 1;
      this.query.pageSize = this.GlobalVar.query.pageSize;
      var SearchType = "";
      //还原选择框数据
      this.options.forEach((element) => {
        if (element.label == this.advancedSearch.SearchType) {
          this.advancedSearch.SearchType = element.value;
        }
      });

      this.options.forEach((element) => {
        if (element.value == this.advancedSearch.SearchType) {
          SearchType = element.label;
        }
      });
      this.advancedSearchList = {
        SearchType: SearchType,
        greaterThan: this.advancedSearch.greaterThan,
      };
      this.tableList();
      this.AdvancedSearch = false;
    },
    closableTag(e) {
      for (const key in this.advancedSearchList) {
        if (e == key) {
          this.advancedSearchList[key] = "";
          this.advancedSearch[key] = "";
        }
      }
      this.query.page = 1;
      this.query.pageSize = this.GlobalVar.query.pageSize;
      this.tableList();
    },
    async tableList() {
      let fd = new FormData();
      let token = window.sessionStorage.getItem("token");
      fd.set("zycmToken", token);
      fd.set("startTime", this.tiemValue[0]);
      fd.set("endTime", this.tiemValue[1]);
      fd.set("clientName", this.inputText);
      fd.set("logNum", this.advancedSearch.greaterThan);
      fd.set("logType", this.advancedSearch.SearchType);
      this.currentTableLoad("media_group_runinglog_table", true, fd);
    },
    empty(type) {
      if (type === 1) {
        this.advancedSearch.greaterThan = "";
        this.advancedSearch.SearchType = "";
      } else {
        this.advancedSearch.greaterThan = "";
        this.advancedSearch.SearchType = "";
        this.advancedSearchList = "";
        this.inputText = "";
        this.tableList();
        this.AdvancedSearch = false;
      }
    },

    // 点击查看
    async LogDetailsShow(e) {
      // if (e) {
      this.logData = e;
      // }
      let formData = new FormData();
      let token = window.sessionStorage.getItem("token");
      formData.append("zycmToken", token);
      formData.append("startTime", this.tiemValue[0]);
      formData.append("endTime", this.tiemValue[1]);
      formData.append("machineCode", this.logData.machineCode);
      formData.append("logType", this.advancedSearch.SearchType);
      this.currentTableLoad("media_group_runinglog_LogDetails", true, formData);
    },
    clientHeight() {
      this.$nextTick(() => {
        this.TerminalHeight = this.$refs.boxTheSize.clientHeight - 95;
      });
    },
    // 转换时间
    dateFormat(time) {
      let date = new Date(time + 8 * 3600000);
      let str = date.toISOString().replace("T", " ");
      return str.substr(0, str.lastIndexOf("."));
    },
  },
};
</script>



<style lang="stylus" scoped>
.Terminal_box {
  .auditOpinion {
    /deep/.el-form-item__label {
      line-height: 80px;
    }

    /deep/.el-form-item__label, /deep/.el-textarea__inner {
      height: 80px;
    }
  }

  .BgColor {
    background-color: #e4f2ff;
    height: 100%;
  }

  /deep/.has-gutter .el-checkbox {
    display: none;
  }

  .tltle_box {
    margin: 1.5% 0;
    background-color: #fff;
    display: flex;
    justify-content: space-between;
    // height: 100px;
    padding 1%
    width: 98%;
    align-items: center;
    margin-bottom: 15px;
    border-radius: 5px;

    .tltle_box_l {
      display: flex;

      .Material_r {
        display: flex;
        // margin-left: 20px;

        /deep/.el-button {
          margin: 0 0 0 20px;
          height: 40px;
          background-color: #2183d8;
        }

        .refresh {
          background-color: #fff;
          color: #2183d8;
        }
      }
    }

    .tltle_box_r {
      padding: 5px;
    }

    /deep/.el-table th, .el-table tr {
      background-color: #fafafa;
    }
  }

  .Terminal_table {
    margin: 1.5% 0;
    width: 100%;
    background-color: #fff;
    border: 0.1px solid #fff;

    .el-table {
      margin: 1.5%;
      width: 97%;
      margin: 20px;
    }
  }

  .Terminal_tree {
    padding: 20px;
  }

  /deep/.el-tree {
    background-color: #e4f2ff;
  }

  .pagination {
    display: flex;
    height: 60px;
    justify-content: center;
  }

  /deep/.el-pager li, /deep/.el-pagination .btn-next, /deep/ .el-pagination .btn-prev {
    border: 1px solid #eee;
    border-radius: 5px;
    margin: 0 5px;
    color: #333;
    padding: 0;
  }

  /deep/.el-pager li.active {
    background-color: #2183d8;
    color: #fff;
  }

  /deep/.el-pagination__sizes {
    padding-left: 20px;
  }

  .fold {
    display: flex;
    width: 100%;
  }

  .fold /deep/.el-form-item__label {
    color: #0095fb;
  }

  .fold /deep/.el-form-item {
    width: 25%;
  }

  /deep/.el-tabs--bottom /deep/.el-tabs__item.is-bottom:nth-child(2), /deep/.el-tabs--bottom .el-tabs__item.is-top:nth-child(2), /deep/.el-tabs--top .el-tabs__item.is-bottom:nth-child(2), /deep/.el-tabs--top .el-tabs__item.is-top:nth-child(2) {
    padding-left: 20px;
  }

  /deep/.el-tabs__item {
    padding: 0 20px;
  }

  /deep/.el-tabs__active-bar {
    padding: 0 20px;
    left: -20px;
    color: #2183d8;
  }

  /deep/.el-table th, .el-table tr {
    background-color: #fafafa;
  }
}

.refreshFor {
  display: flex;
  margin: 0 10px;

  .el-tag {
    max-width: 120px;
    height: 40px;
    line-height: 40px;
    overflow: hidden;
    margin-right: 10px;

    /deep/.el-icon-close {
      top: -15px;
    }

    .refreshFor_li_text {
      display: inline-block;
      max-width: 80px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}

.LogDetails {
  /deep/.el-dialog__body {
    padding: 10px 20px;
  }
}

/deep/.el-dialog {
  .Advanced_head {
    overflow: hidden;
    margin: 0 0 20px;

    div:nth-child(1) {
      float: left;
      width: 250px;
    }

    div:nth-child(2) {
      float: right;
      width: 250px;
    }
  }

  .inputTransparent {
    /deep/.el-form-item__content {
      display: flex;
      // justify-content: space-around
      text-align: center;
    }
  }

  .first_box {
    height: 250px;
    width: 99%;
    border: 1px solid #eee;
  }

  .auditOpinion {
    /deep/.el-form-item__label {
      line-height: 120px;
    }

    /deep/.el-form-item__label, /deep/.el-textarea__inner {
      height: 120px;
    }
  }

  /deep/.el-tree {
    background-color: #fff;
    padding: 0 0 5% 10%;
  }

  .selectTerminal {
    /deep/.el-tree {
      background-color: #fff;
      padding: 0 0 5% 10%;
    }
  }

  /deep/.el-select {
    display: block;
  }

  /deep/.el-dialog__header {
    background-color: #efefef;
    padding: 0;
    line-height: 50px;
    padding-left: 20px;
    color: #333;
    font-size: 16px;
  }

  /deep/.el-form-item__label {
    background-color: #efefef;
    border-radius: 0;
    height: 40px;
    border: 1px solid #dcdfe6;
    border-right: none;
    box-sizing: border-box;
    text-align: center;
    padding-left: 10px;
  }

  /deep/.el-input__inner, /deep/.el-textarea__inner {
    border-radius: 0;
  }

  /deep/.el-button {
    padding: 12px 25px;
  }

  .opinion {
    /deep/.el-form-item__label {
      height: 80px;
      line-height: 80px;
    }
  }
}
</style>
